 --package.path = "lualib/?.lua;server_websocket/?.lua"
--
-- 南昌玩法(上下精)
-- @author yangzhengwu<iokdo2012@163.com>
-- @date 2016-08-20
--

--cc.exports.MJLogic = {}

local MJLogic = {}
local table_insert = table.insert
local table_remove = table.remove

--local json   = require "cjson"

-- 统计
MJLogic.HS_ZIMO				= 1		-- 自摸(小胡)
MJLogic.HS_JIEPAO			= 2		-- 自摸(小胡)
MJLogic.HS_DIANPAO			= 3		-- 自摸(小胡)
MJLogic.HS_MINGGANG			= 4		-- 自摸(小胡)
MJLogic.HS_ANGANG			= 5		-- 自摸(小胡)
MJLogic.HS_DA_ZIMO			= 6		-- 自摸(小胡)
MJLogic.HS_DA_JIEPAO		= 7		-- 自摸(小胡)
MJLogic.HS_DA_DIANPAO		= 8		-- 自摸(小胡)

-- 牌型
MJLogic.CT_WANZI            = 0x00      --  万子
MJLogic.CT_TONGZI           = 0x10      --  同子
MJLogic.CT_SUOZI            = 0x20      --  索子
MJLogic.CT_FENGPAI          = 0x30      --  风牌
MJLogic.CT_JIANPAI          = 0x40      --  箭牌
MJLogic.CT_HUAPAI           = 0x50      --  花牌

-- 胡牌
MJLogic.HU_NONE         = 0	
MJLogic.HU_NORMAL       = 1	
MJLogic.HU_PENGPENGHU	= 2	-- 碰碰胡
MJLogic.HU_QIXIAODUI	= 3	-- 七小对	
MJLogic.HU_SHISANLAN	= 4	-- 十三烂
MJLogic.HU_QXSHISANLAN	= 5	-- 七星十三烂
MJLogic.HU_ZIMO			= 6	-- 自摸
MJLogic.HU_JINGDIAO		= 7 -- 精钓
MJLogic.HU_GANGKAI		= 8 -- 杠开
MJLogic.HU_QIANGGANG	= 9 -- 抢杠
MJLogic.HU_DEIGUO		= 10 -- 德国
MJLogic.HU_DEIZHONGDEI	= 11 -- 德中德
MJLogic.HU_TIANHU		= 12 -- 天胡
MJLogic.HU_DIHU			= 13 -- 地胡
MJLogic.HU_ZHUANGJIA	= 14 -- 庄家
MJLogic.HU_DIANPAO		= 15 -- 点炮

MJLogic.HU_GANG_FANG		= 94 -- 放杠
MJLogic.HU_GANG_JIE		= 95 -- 接杠
MJLogic.HU_GANG_GONG			= 96 -- 公杠
MJLogic.HU_GANG_AN			= 97 -- 暗杠
MJLogic.HU_GANG_SHANG_PAO			= 98 -- 杠上炮
MJLogic.ZHUI_ZHUANG			= 99 -- 跟拍追庄



MJLogic.MA_JIANG_CARD = {
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万子  
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万子  
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万子  
    0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,                       --万子  
    0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       --同子  17,18,19,20,21,22,23,24,25,26
	--17,--18,--19,--20,--21,--22,--23,--24,--25,
    0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       --同子  
    0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       --同子  
    0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,                       --同子  
    0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       --索子  
    --33,--34,--35,--36,--37,--38,--39,--40,--41,
	0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       --索子  
    0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       --索子  
    0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,                       --索子  
    
	--49,--50,--51,--52
    0x31,0x32,0x33,0x34,                                                --风牌(东南西北)
    0x31,0x32,0x33,0x34,                                                --风牌  
    0x31,0x32,0x33,0x34,                                                --风牌  
    0x31,0x32,0x33,0x34,                                                --风牌  
	--65,--66,--67
    0x41,0x42,0x43,                                                     --箭牌(中发白)
    0x41,0x42,0x43,                                                     --箭牌  
    0x41,0x42,0x43,                                                     --箭牌  
    0x41,0x42,0x43,                                                     --箭牌  
}
    --0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,                            --花牌(春夏秋冬梅兰竹菊)

function MJLogic.print_cards(arr)
    for i,v in MJLogic.pairsByKeys(arr) do
        if v > 0 then
            print(string.format("0x%02x   %d",i, v))
        end
    end
end

-- 返回一个顺序的迭代器
-- 输入: 统计牌数的表
function MJLogic.pairsByKeys(t) 
    local a = {} 
    for n in pairs(t) do 
        a[#a+1] = n 
    end 
    table.sort(a) 
    local i = 0 
    return function() 
        i = i + 1 
        return a[i], t[a[i]] 
    end 
end

function MJLogic.has_card(cards, tbl)
    local count = 0
    local data = {}
    for k,v in ipairs(tbl) do
        if not data[v] then
            data[v] = 0
        end
        data[v] = data[v] + 1
        count = count + 1
    end

    for i = #cards, 1, -1 do
        local v = cards[i]
        if data[v] and data[v] > 0 then
            data[v] = data[v] - 1
            count = count - 1
        end
    end
    return count == 0
end

function MJLogic.NextCard(card)
	local color = MJLogic.GetColor(card)
	local value = MJLogic.GetValue(card)
	local next_value = 0
	if color == 0 or color == 16 or color == 32 then
		next_value = (value == 9 and 1 or value + 1)
	elseif color == 48 then
		next_value = (value == 4 and 1 or value + 1)
	elseif color == 64 then
		next_value = (value == 3 and 1 or value + 1)
	end
	return tonumber(color  + next_value)
end

function MJLogic.PrevCard(card)
	local color = MJLogic.GetColor(card)
	local value = MJLogic.GetValue(card)
	local next_value = 0
	if color == 0 or color == 16 or color == 32 then
		next_value = (value == 1 and 9 or value - 1)
	elseif color == 48 then
		next_value = (value == 1 and 4 or value - 1)
	elseif color == 64 then
		next_value = (value == 1 and 3 or value - 1)
	end
	return tonumber(color + next_value)
end

function MJLogic.Shaizi()
	local shaizi = {}
	shaizi[1] = math.random(1, 6)
	shaizi[2] = math.random(7, 12) - 6
	return shaizi
end

function MJLogic.delete_card(cards, tbl)
    local remove = {}
    for k,v in ipairs(tbl) do
        if not remove[v] then
            remove[v] = 0
        end
        remove[v] = remove[v] + 1
    end

    for i = #cards, 1, -1 do
        local v = cards[i]
        if remove[v] and remove[v] > 0 then
            table_remove(cards, i)
            remove[v] = remove[v] - 1
        end
    end
end

function MJLogic.sortASCE( t )
    table.sort(t)
end

function MJLogic.sortDESC( t )
    table.sort(t,function() return a > b end)
end

-- 获取花式
function MJLogic.GetColor( data )
    --return cc_math:bit_and(data, 0xF0)
    return data & 0xF0
	--local color,_ = math.modf(data/16)
	--return color
end

-- 获取数值
function MJLogic.GetValue( data )
    --return cc_math:bit_and(data, 0x0F)
    return data & 0x0F
	--return data % 16
end

-- 判断剩余牌的数量
function MJLogic.RemainCard(arr)
    local sum = 0
    for i,v in pairs(arr) do
        sum = sum + v
    end
    return sum
end

-- 统计手牌的各个牌的数量
function MJLogic.GetHandCount( data )
    local hand_count = {}
    for i,v in ipairs(data) do
        if hand_count[v] == nil then
            hand_count[v] = 1
        else
            hand_count[v] = hand_count[v] + 1
        end
    end
    return hand_count
end

function MJLogic.has_card_num(cards, card)
	local count = 0
	for i = #cards, 1, -1 do
        local v = cards[i]
        if v == card then
            count = count + 1
        end
    end
	return count
end

-- 统计手上牌的颜色数量
function MJLogic.GetHandColor( data) 
	local color_count = {}
	local count = 0
	for i,v in ipairs(data) do
		local color = MJLogic.GetColor(v)
		if color_count[color] == nil then
			color_count[color] = 1
			count = count + 1
		else
			color_count[color] = color_count[color] + 1
		end
	end
	return count
end

-- 获取所有对子
function MJLogic.GetDuizi(data)
    local duizi = {}
    local hand_count = MJLogic.GetHandCount(data)
    for i, num in MJLogic.pairsByKeys(hand_count) do
        if num >= 2 and num < 4 then
            table.insert(duizi, i)
        elseif num == 4 then
            table.insert(duizi, i)
            table.insert(duizi, i)
        end
    end
    return duizi
end

-- 手上的杠牌
function MJLogic.GetGangPai(data)
    local gangs = {}
    local hand_count = MJLogic.GetHandCount(data)
    for i, num in MJLogic.pairsByKeys(hand_count) do
        if num == 4 then
            table.insert(gangs, i)
        end
    end
    return gangs
end

function MJLogic.GetCanGang(hand,group, cant_gang_cards)
	local gang_data = MJLogic.GetGangPai(hand)
	if #gang_data > 0 then
		return gang_data[1],1
	end
	-- 手中的每个牌跟group判断	
	local gang_card = MJLogic.GetGroupGangpai(hand,group, cant_gang_cards)
	if gang_card > 0 then
		for i,v in ipairs(cant_gang_cards) do 
			if v == gang_card then 
				return nil,0
			end
		end
		return gang_card,3
	end
	return nil,0
end

-- 暗杠
function MJLogic.GetGroupGangpai(hand,group)
	for i,card in ipairs(hand) do
		for i,g in ipairs(group) do
			if card == g[1] and card == g[2] and card == g[3] then
				return card
			end
		end
	end	
	return 0
end


-- 判断是否可以碰
function MJLogic.CanPeng(data, cant_peng_cards, out_card)
	
	if #cant_peng_cards > 0 then 
		for i,v in ipairs(cant_peng_cards) do 
			if v == out_card then 
				return false
			end
		end
	end
	
    local duizi = MJLogic.GetDuizi(data)
    for i,v in ipairs(duizi) do
        if v == out_card then
            return true
        end
    end
    return false
end

-- 判断是否可以杠
function MJLogic.CanGang(data, card)
    local hand_count = MJLogic.GetHandCount(data)
    for i, num in MJLogic.pairsByKeys(hand_count) do
        if i == card and num == 3 then
            return true
        end
    end
    return false
end

-- 判断是否可以吃牌(组成顺子)
function MJLogic.CanChi( data, card, ret)
    if ret == nil then
        ret = {}
    end
    local hand_count = MJLogic.GetHandCount(data)
    if hand_count[card-2] and hand_count[card-1] then
        table.insert(ret, {card-2,card,card-1})
    end
    if hand_count[card-1] and hand_count[card+1] then
        table.insert(ret, {card-1, card, card+1})
    end
    if hand_count[card+1] and hand_count[card+2] then
        table.insert(ret, {card+1, card, card+2})
    end
	
	if card >= 0x31 and card <= 0x34 then 
		if hand_count[card+1] and hand_count[card+3] then
			table.insert(ret, {card+1,card,card+3})
		end
		if hand_count[card+2] and hand_count[card+3] then
			table.insert(ret, {card+2,card,card+3})
		end
		if hand_count[card-2] and hand_count[card+1] then
			table.insert(ret, {card-2,card,card+1})
		end
		if hand_count[card-1] and hand_count[card+2] then
			table.insert(ret, {card-1,card,card+2})
		end
		if hand_count[card-3] and hand_count[card-2] then
			table.insert(ret, {card-3,card,card-2})
		end
		if hand_count[card-3] and hand_count[card-1] then
			table.insert(ret, {card-3,card,card-1})
		end
	end
	
    if #ret > 0 then
        return true
    end
    return false
end

-- 判断是否可以吃胡
function MJLogic.CanChiHu( data, group_data, card, zj, fj)
	local new_data = {}
	for i,v in ipairs(data) do
		table.insert(new_data,v)
	end
	local new_data2 = {}
	for i,v in ipairs(group_data) do
		table.insert(new_data2,v)
	end
	local can_hu = MJLogic.JudgeHu(new_data, new_data2, zj, fj, card)
	return can_hu > 0 and true or false
end

-- 判断自摸
function MJLogic.CanZimoHu( data, group_data, zj, fj)
	local new_data = {}
	for i,v in ipairs(data) do
		table.insert(new_data,v)
	end
	local new_data2 = {}
	for i,v in ipairs(group_data) do
		table.insert(new_data2,v)
	end
    if MJLogic.JudgeHu(new_data, new_data2, zj, fj) > 0 then
        return true
    end
    return false
end


function MJLogic.JudgeHu_Duidui( data, zj, fj, card)
    if #data < 14 then return MJLogic.HU_NONE end
	if zj or fj then
		local arr = {}
		local jing_num = 0
		local z  = zj and zj or 0
		local f  = fj and fj or 0
		for i,v in ipairs(data) do
			if v == z or v == f then
				jing_num = jing_num + 1
			else
				arr[v] = arr[v] and arr[v] + 1 or 1
			end
		end	
		
		if card then 
			if arr[card] > 0 then 
				arr[card] = arr[card] + 1
			else
				arr[card] = 1
			end
		end
		
		
		local kuan = 0
		local comb = {}
		local function DuiDui()
			if MJLogic.RemainCard(arr) == 0 then return 1 end
			for i,num in MJLogic.pairsByKeys(arr) do
				if num > 0 then
					if num >= 2 then
						arr[i] = arr[i] - 2  
						--table.insert(comb,{i,i})
						if DuiDui() == 1 then return 1 end 
						arr[i] = arr[i] + 2 
						--table.remove(comb)
					end
					
					if jing_num > 0 and num == 1 then
						arr[i] = 0
						table.insert(comb,{i,0})
						jing_num = jing_num - 1
						if DuiDui() == 1 then return 1 end 
						arr[i] = 1
						table.remove(comb)
						jing_num = jing_num + 1
					end
				end
			end
			return 0
		end
		if DuiDui() == 1 then
			return MJLogic.HU_QIXIAODUI,comb
		else
			return MJLogic.HU_NONE,{}
		end
	else
		local new_data = clone(data)
		if card then 
			table.insert(new_data, card)
		end
		local duizi = MJLogic.GetDuizi(new_data)
		if #duizi == 7 then
			local gangs = MJLogic.GetGangPai(new_data)
			if #gangs > 0 then
				return MJLogic.HU_HAOQIXIAODUI
			else
				return MJLogic.HU_QIXIAODUI
			end
		end
		return MJLogic.HU_NONE
	end
end

function MJLogic.JudgeHu_PENGPENGHU(data, group, zj, fj, card) 
	if group and #group > 0 then
		for i,v in ipairs(group) do
			if v[1] ~= v[2] or v[1] ~= v[3] then
				return MJLogic.HU_NONE
			end
		end
	end
	local ret,combs = MJLogic.JudgeHu_Normal(data, zj, fj, true, card)      -- 普通胡
	if ret == MJLogic.HU_NORMAL then 
		return MJLogic.HU_PENGPENGHU,combs
	else
		return MJLogic.HU_NONE,{}
	end
	--return ret == MJLogic.HU_NORMAL and MJLogic.HU_PENGPENGHU or MJLogic.HU_NONE
end

-- 13烂  
function MJLogic.JudgeHu_SHISANLAN( data , zj, fj, card)
	if #data < 13 then
		return	MJLogic.HU_NONE
	end
	local arr = {}
    for i,v in ipairs(MJLogic.MA_JIANG_CARD) do
        arr[v] = 0
    end
    local jing_num = 0  -- 精牌数量
    for i,v in ipairs(data) do
        if v ~= zj and v ~= fj then 
			arr[v] = arr[v] + 1
		end
        if ( zj and v == zj) or (fj and v == fj)  then
            jing_num = jing_num + 1 -- 精牌数量
		end
    end
	
	if card then 
		if arr[card] > 0 then 
			arr[card] = arr[card] + 1
		else
			arr[card] = 1
		end
	end

	local zi_num = 0
    local jing_tesu = 0  -- 一个精的特殊情况,,万 同 条 都是 3 7 或者 3 8
	
    for i, num in MJLogic.pairsByKeys(arr) do           -- 找到有牌的地方，i就是当前牌，arr[i]是个数 
		if num > 0 then 
			if num >= 2 then return MJLogic.HU_NONE end
			if i >= 0x31 then
				zi_num = zi_num + 1	
			else
				local value = MJLogic.GetValue(i)
				if value < 8 and (arr[i+1] > 0 or arr[i+2] > 0) then
					return 	MJLogic.HU_NONE
				elseif value == 8 and arr[i+1]> 0 then
					return 	MJLogic.HU_NONE
				end

				if jing_num == 1 and (value == 3 or value == 7 or value == 8) then
					jing_tesu = jing_tesu + 1
					if jing_tesu > 5 then
						return  MJLogic.HU_NONE
					end
				end
			end 
		end
	end 

	if zi_num >= 7 then
		return MJLogic.HU_QXSHISANLAN
	else
		return MJLogic.HU_SHISANLAN
	end
end

function MJLogic.JudgeHu_JINGDIAO(combs, card, hu_types)
	if #combs > 0 then
		for i,v in pairs(combs) do
			if v[1] == 0 or v[1] == card then
				table.insert(hu_types, MJLogic.HU_JINGDIAO)
				return
			end
		end
	end
end

function MJLogic.JudgeHu_DEIGUO(data, bawang, card)
	local hu_type = 0
    hu_type = MJLogic.JudgeHu_Duidui(data,nil,nil,card)      
    if hu_type > 0 then 
		return bawang and MJLogic.HU_DEIZHONGDEI or MJLogic.HU_DEIGUO
	end
	
	hu_type = MJLogic.JudgeHu_SHISANLAN(data,nil,nil,card)  
    if hu_type > 0 then 
		return bawang and MJLogic.HU_DEIZHONGDEI or MJLogic.HU_DEIGUO
	end
	
    hu_type = MJLogic.JudgeHu_Normal(data,nil,nil,false,card)
    if hu_type > 0 then 
		return bawang and MJLogic.HU_DEIZHONGDEI or MJLogic.HU_DEIGUO
	end
	return MJLogic.HU_NONE
end

function MJLogic.JudgeHuTypes(data, group, zhengjing, fujing, hu_types, hu_card, bawang, card)
	local types = hu_types and hu_types or {}
	local hu_type = 0
	local combs = {}
	local new_data = {}
	for i,v in ipairs(data) do
		table.insert(new_data,v)
	end
	hu_type,combs = MJLogic.JudgeHu_Duidui(new_data, zhengjing, fujing, card)
	if hu_type > 0 then 
		MJLogic.JudgeHu_JINGDIAO(combs, hu_card, hu_types)
		table.insert(types,hu_type) 
	end
	
	hu_type,combs = MJLogic.JudgeHu_PENGPENGHU(new_data, group, zhengjing, fujing, card)
	if hu_type > 0 then 
		table.insert(types,hu_type) 
	end
		
	hu_type = MJLogic.JudgeHu_SHISANLAN(new_data, zhengjing, fujing, card)
	if hu_type > 0 then table.insert(types,hu_type) end
	
	if #types == 0 then
		hu_type,combs = MJLogic.JudgeHu_Normal(new_data, zhengjing, fujing, false, card)
		MJLogic.JudgeHu_JINGDIAO(combs, hu_card, hu_types)
	end
	
	hu_type = MJLogic.JudgeHu_DEIGUO(new_data, bawang, card)
	if hu_type > 0 then
		table.insert(types,hu_type)
	end
	return types
end

function MJLogic.JudgeHu( data, group, zj, fj, card)
	local hu_type = 0
    hu_type = MJLogic.JudgeHu_Duidui(data, zj, fj, card)      
    if hu_type > 0 then 
		return hu_type 
	end
	
	hu_type = MJLogic.JudgeHu_PENGPENGHU(data, group, zj, fj, card) 
    if hu_type > 0 then 
		return hu_type 
	end
	
	hu_type = MJLogic.JudgeHu_SHISANLAN(data, zj, fj, card)  
    if hu_type > 0 then 
		return hu_type 
	end
	
    hu_type = MJLogic.JudgeHu_Normal(data, zj, fj, false, card)
    if hu_type > 0 then 
		return hu_type 
	end

    return MJLogic.HU_NONE
end


-- 判断普通胡
--[[  
    data: 手上的牌
	zj: 主精
	fj: 副精
	noshunzi: 没有顺子(碰碰胡)
--]]  
function MJLogic.JudgeHu_Normal( data , zj, fj, noshunzi, card)
    local arr = {}
    local jiang = 0     -- 将牌(眼牌)
	local jing_num = 0	-- 精牌数量
	local jiang_cards = {}
    for i,v in ipairs(MJLogic.MA_JIANG_CARD) do
        arr[v] = 0
    end
	local z  = zj and zj or 0
	local f  = fj and fj or 0
    for i,v in ipairs(data) do
		if v == z or v == f then
			jing_num = jing_num + 1
		else
			arr[v] = arr[v] + 1
		end
    end
	
	if card then 
		if arr[card] > 0 then 
			arr[card] = arr[card] + 1
		else
			arr[card] = 1
		end
	end
	
    local function NormalHu()
        if MJLogic.RemainCard(arr) == 0 then return 1 end   -- 递归退出条件：如果没有剩牌，则胡牌返回
        for i, num in MJLogic.pairsByKeys(arr) do
            if num > 0 then
                local value = MJLogic.GetValue(i)
				-- 刻子
				if arr[i] >= 3 then    
                    arr[i] = arr[i] - 3    
                    if NormalHu() == 1 then return 1 end
                    arr[i] = arr[i] + 3  
                end
				
				-- 2张组合
                if jiang == 0 and arr[i] >= 2 then
					jiang = 1    
					arr[i] = arr[i] - 2  
					--jiang_cards = {i,i}
					if NormalHu() == 1 then return 1 end 
					arr[i] = arr[i] + 2  
					--jiang_cards = {}
					jiang = 0    
                end
				
				-- 刻子,用一张“精”
				if jing_num >= 1 and arr[i] == 2 then
					arr[i] = arr[i] - 2
					jing_num = jing_num - 1
					if NormalHu() == 1 then return 1 end
                    arr[i] = arr[i] + 2
					jing_num = jing_num + 1
				end
				
				-- 2张组合(将牌,用一张精)
				if jiang == 0 and jing_num >= 1  and arr[i] == 1  then
					jiang = 1 
					arr[i] = arr[i] - 1 
					jing_num = jing_num - 1
					jiang_cards = {{i,0}}
					if NormalHu() == 1 then return 1 end 
					arr[i] = arr[i] + 1  
					jing_num = jing_num + 1
					jiang_cards = {}
					jiang = 0 
				end 
				
				
				
                -- 顺牌组合
                if i < 0x31 and not noshunzi and value ~= 8 and value ~= 9 and arr[i+1] > 0 and arr[i+2] > 0 then
                    arr[i] = arr[i] - 1
                    arr[i+1] = arr[i+1] - 1
                    arr[i+2] = arr[i+2] - 1
                    if NormalHu() == 1 then return 1 end
                    arr[i] = arr[i] + 1
                    arr[i+1] = arr[i+1] + 1
                    arr[i+2] = arr[i+2] + 1
                end
				
				-- 后面第一张用“精”
				if i < 0x31 and not noshunzi and jing_num >= 1 and value ~= 8 and value ~= 9 and arr[i+2] > 0 then
                    arr[i] = arr[i] - 1
					arr[i+2] = arr[i+2] - 1
					jing_num = jing_num - 1
                    if NormalHu() == 1 then return 1 end
                    arr[i] 		= arr[i] + 1
                    arr[i+2] 	= arr[i+2] + 1
					jing_num 	= jing_num + 1
                end
				
				-- 后面第二张用“精”
				if i < 0x31 and not noshunzi and jing_num >= 1 and value ~= 9 and arr[i+1] > 0 then
                    arr[i] = arr[i] - 1
					arr[i+1] = arr[i+1] - 1
					jing_num = jing_num - 1
                    if NormalHu() == 1 then return 1 end
                    arr[i] 		= arr[i] + 1
                    arr[i+1] 	= arr[i+1] + 1
					jing_num 	= jing_num + 1
                end
				
				-- 中发白  东南西   南西北
				if not noshunzi and (i == 0x41 or i == 0x31 or i == 0x32) and arr[i+1] > 0 and arr[i+2] > 0 then 
					arr[i] = arr[i] - 1
                    arr[i+1] = arr[i+1] - 1
                    arr[i+2] = arr[i+2] - 1
                    if NormalHu() == 1 then return 1 end
                    arr[i] = arr[i] + 1
                    arr[i+1] = arr[i+1] + 1
                    arr[i+2] = arr[i+2] + 1
				end
				
				-- 东南北
				if not noshunzi and i == 0x31 and arr[i+1] > 0 and arr[i+3] > 0 then 
					arr[i] = arr[i] - 1
                    arr[i+1] = arr[i+1] - 1
                    arr[i+3] = arr[i+3] - 1
                    if NormalHu() == 1 then return 1 end
                    arr[i] = arr[i] + 1
                    arr[i+1] = arr[i+1] + 1
                    arr[i+3] = arr[i+3] + 1
				end
				
				-- 东西北
				if not noshunzi and i == 0x31 and arr[i+2] > 0 and arr[i+3] > 0 then 
					arr[i] = arr[i] - 1
                    arr[i+2] = arr[i+2] - 1
                    arr[i+3] = arr[i+3] - 1
                    if NormalHu() == 1 then return 1 end
                    arr[i] = arr[i] + 1
                    arr[i+2] = arr[i+2] + 1
                    arr[i+3] = arr[i+3] + 1
				end
				
				-- 东南西北，2个字
				if not noshunzi and jing_num >= 1 and i >= 0x31 and i < 0x34 then 
					for k=1,0x34-i do 
						local tmp = i + k
						if arr[i] > 0 and arr[tmp] > 0 and tmp <= 0x34 then 
							arr[i] = arr[i] - 1
							arr[tmp] = arr[tmp] - 1
							jing_num = jing_num - 1
							if NormalHu() == 1 then return 1 end
							arr[i] 		= arr[i] + 1
							arr[tmp] 	= arr[tmp] + 1
							jing_num 	= jing_num + 1
						end
					end
				end
				
				-- 中发白
				if not noshunzi and jing_num >= 1 and i >= 0x41 and i < 0x43 then 
					for k=1,0x43-i do 
						local tmp = i + k
						if arr[i] > 0 and arr[tmp] > 0 and tmp <= 0x43 then 
							arr[i] = arr[i] - 1
							arr[tmp] = arr[tmp] - 1
							jing_num = jing_num - 1
							if NormalHu() == 1 then return 1 end
							arr[i] 		= arr[i] + 1
							arr[tmp] 	= arr[tmp] + 1
							jing_num 	= jing_num + 1
						end
					end
				end
				
				
				-- 刻子,用2张“精”
				if jing_num >= 2 and arr[i] == 1 then
					arr[i] = arr[i] - 1
					jing_num = jing_num - 2
					local tag = false
					if #jiang_cards == 0 then 
						jiang_cards = {{i,0}}
						tag = true
					end
					
					if NormalHu() == 1 then return 1 end 
                    arr[i] = arr[i] + 1 
					jing_num = jing_num + 2
					if tag == true then 
						jiang_cards = {}
					end
				end
				
				-- 2张组合(将牌,用两张“精”)
				if jiang == 0 and jing_num >= 2 then
					jiang = 1               
					jing_num = jing_num - 2
					jiang_cards = {{0,0}}
					if NormalHu() == 1 then return 1 end  
					jing_num = jing_num + 2
					jiang_cards = {}
					jiang = 0 
				end
				
				
				-- (刻子,用3张“精”)
				if jing_num >= 3 then
					jing_num = jing_num - 3
					if NormalHu() == 1 then return 1 end    -- 如果剩余的牌组合成功，胡牌
					jing_num = jing_num + 3
				end
				
                return 0
            end
        end
    end

    if NormalHu() == 1 then
        return MJLogic.HU_NORMAL,jiang_cards
    else
        return MJLogic.HU_NONE,jiang_cards
    end
end


return MJLogic
